package projbd;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SQLRunner {
    
    private static Connection connection = null;
    
    public static void main(String[] args) throws SQLException {
        
        /* BD Connection */
                
        try {           
            Class.forName("org.postgresql.Driver");         
            connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/projbd", "postgres", "srx3325");
            if(connection != null) {
                System.out.println("CONNECTED !!!");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ProjBD.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ProjBD.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        //String sql0 = "COPY order_size_edges_2 FROM 'C:\\bd\\statements_2.sql' USING DELIMITERS '!' WITH NULL AS '#null';";
        //String sql1 = "ALTER TABLE order_size_edges_2 ADD PRIMARY KEY (id)";        
        //String sql2 = "ALTER TABLE order_size_edges_2 ADD CONSTRAINT parent FOREIGN KEY (parent_id) REFERENCES order_size_edges (id) MATCH FULL";  
        //String sql3 = "CREATE INDEX value_idx_2 ON order_size_edges_2(value)";
        //String sql4 = "CREATE INDEX father_idx_2 ON order_size_edges_2(parent_id)";
        
        /*
        String sql0 = "COPY order_size_edges FROM 'C:\\bd\\statements.sql' USING DELIMITERS '!' WITH NULL AS '#null';";
        String sql1 = "ALTER TABLE order_size_edges ADD PRIMARY KEY (id)";
        String sql3 = "CREATE INDEX value_idx ON order_size_edges(value)";
        String sql4 = "CREATE INDEX father_idx ON order_size_edges(parent_id)";
        */
        
        String sqlUpdate = "COPY order_size_edges_2 FROM 'C:\\bd\\updates.sql' USING DELIMITERS '!' WITH NULL AS '#null';";
        
        
        try {
            Statement statement = connection.createStatement();
            
            //statement.execute(sql0);
            //statement.execute(sql1);
            //statement.execute(sql2);
            //statement.execute(sql3);
            
            statement.execute(sqlUpdate);
            
            
            /*
            System.out.println("COPY");
            statement.execute(sql0);
            
            System.out.println("PK");
            statement.execute(sql1);
            
            System.out.println("FK");
            statement.execute(sql2);
            
            System.out.println("value_idx");
            statement.execute(sql3);
            
            System.out.println("father_idx");
            statement.execute(sql4);
            */
            
            statement.executeBatch();
            
        } catch (SQLException ex) {
            Logger.getLogger(OrderSizeNumSchemeXMLParser.class.getName()).log(Level.SEVERE, null, ex);
        }     
              
        /* BD Close Connection */
        connection.close();  
    }

    /* Just provide the root node and the whole subtree is deleted */
    public void delete(String id) {
        String delete = "DELETE FROM order_size_edges_2 CASCADE WHERE id = " + "'" + id + "'";
        try {
            Statement deleteStatement = connection.createStatement();
            deleteStatement.executeUpdate(delete);
        } catch (SQLException ex) {
            Logger.getLogger(XMLParser.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(delete);            
        }        
    }   
}
